//
//  NSObject+PrintIvars.swift
//  表情键盘
//
//  Created by whtapple on 4/5/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import Foundation
/// 返回值对应表
let ivarTypeDict = [":": "SEL", "#": "Class", "@": "id", "*": "char *", "v": "Void", "B": "Bool", "d": "Double", "f": "Float", "Q": "Long", "L": "Long", "S": "Short", "I": "Int", "C": "Char", "q": "Long", "c": "Char", "i": "Int", "s": "Short", "l": "Long"]
extension NSObject{
    class func printIvars(){
        //参数
        var count: UInt32 = 0
        //得到 ivars
        let ivars = class_copyIvarList(self, &count)
        print("开始答应")
        for i in 0..<count{
            //得到其中的一个
            let ivar = ivars[Int(i)]
            //拿出名字
            let cName = ivar_getName(ivar)
            //转化为字符创
            let name = String(CString: cName, encoding: NSUTF8StringEncoding)!
             //拿到类型
            let cType = ivar_getTypeEncoding(ivar)
            //转化为字符串
            var type = String(CString: cType, encoding: NSUTF8StringEncoding)!
            if let newtype = ivarTypeDict[type]{
                type = newtype
            }
            print("name:\(name),type:\(type)")
        }
        print("结束答应")
    }
}